from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtWidgets import QMainWindow

from UI import login
from UI import Main

class MainWindow(QMainWindow,login.Ui_MainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.drag_position = None
        self.bindEvents()

    #进行登录窗口的事件绑定
    def bindEvents(self):
        self.pushButton.clicked.connect(self.login)

    #下面三个函数用于实现拖动窗口的效果
    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.drag_position = event.globalPos() - self.pos()
            event.accept()
            self.setCursor(QtCore.Qt.OpenHandCursor)

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.drag_position)
            event.accept()

    def mouseReleaseEvent(self, event):
        self.drag_position = None
        self.setCursor(QtCore.Qt.ArrowCursor)


    #登录按钮所执行的操作
    def login(self):
        print('login')
        user = self.lineEdit.text()
        password = self.lineEdit_2.text()
        if user == 'admin' and password == 'admin':
            self.label_4.setText('登录成功')
            self.label_4.setText("")
            self.m = InterfaceWindow()
            self.m.show()
            self.close()
        else:
            self.label_4.setText('登录失败')
            self.label_4.setText("")
class InterfaceWindow(QMainWindow,Main.Ui_MainWindow):
    def __init__(self):
        super(InterfaceWindow,self).__init__()
        self.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.bindEvent()

    #绑定相关事件
    def bindEvent(self):
        self.pushButton_info.clicked.connect(lambda :self.stackedWidget.setCurrentIndex(0))
        self.pushButton_img.clicked.connect(lambda:self.stackedWidget.setCurrentIndex(1))
        self.pushButton_logout.clicked.connect(self.logout)

    #退出登录按钮的操作
    def logout(self):
        MainWindow.show() #显示登录窗口
        MainWindow.lineEdit.setText("")
        MainWindow.lineEdit_2.setText("")
        self.close()    #关闭当前窗口


    #以下三个函数用于实现窗口拖动效果
    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.drag_position = event.globalPos() - self.pos()
            event.accept()
            self.setCursor(QtCore.Qt.OpenHandCursor)

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.drag_position)
            event.accept()

    def mouseReleaseEvent(self, event):
        self.drag_position = None
        self.setCursor(QtCore.Qt.ArrowCursor)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())


